﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Annotations;
using OxyPlot.Series;

namespace PlayPlot
{
    /// <summary>
    /// Interaction logic for ArrowDemoWin.xaml
    /// </summary>
    public partial class ArrowDemoWin : Window
    {
        private readonly OxyColor[] _colors;
        private readonly DataPoint _zeroPnt;

        public PlotModel PlotModel { get; private set; }

        public ArrowDemoWin()
        {
            InitializeComponent();

            _zeroPnt = new DataPoint(0, 0);

            _colors = new OxyColor[]
            {
                OxyColors.Red,
                OxyColors.Orange,
                OxyColors.Blue,
                OxyColors.Green,
                OxyColors.SkyBlue,
                OxyColors.Purple,
                OxyColors.GreenYellow,
                OxyColors.Crimson
            };

            PlotModel = new PlotModel("Arrow Demonstration")
            {
                PlotMargins = new OxyThickness(10),
            };
            InitXYAxes();
            InitPolarAxes();
            DrawArrows(MakePoints(4));

            this.DataContext = this;
        }

        private void InitXYAxes()
        {
            for (int index = 0; index < 2; index++)
            {
                var axis = new LinearAxis
                {
                    Maximum = 1,
                    Minimum = -1,
                    MajorStep = 0.2,
                    MinorStep = 0.1,
                    PositionAtZeroCrossing = true,
                    TickStyle = TickStyle.Crossing
                };
                PlotModel.Axes.Add(axis);
            }
            PlotModel.Axes[0].Position = AxisPosition.Left;
            PlotModel.Axes[1].Position = AxisPosition.Bottom;
        }

        private void InitPolarAxes()
        {
            const LineStyle linestyle = LineStyle.Dot;
            var color = OxyColors.LightGray;
            const int thickness = 2;

            var circle = new LineSeries
            {
                LineStyle = linestyle,
                Color = color,
                StrokeThickness = thickness
            };
            PlotModel.Series.Add(circle);
            for (double ang = 0; ang <= Math.PI * 2; ang += Math.PI / 360)
            {
                var pnt = new DataPoint(Math.Cos(ang), Math.Sin(ang));
                circle.Points.Add(pnt);
            }

            for (double ang = 0; ang < Math.PI * 2; ang += Math.PI / 6)
            {
                var pnt = new DataPoint(Math.Cos(ang), Math.Sin(ang));
                PlotModel.Annotations.Add(new ArrowAnnotation
                {
                    StartPoint = _zeroPnt,
                    EndPoint = pnt,
                    Color = color,
                    LineStyle = linestyle,
                    StrokeThickness = thickness,
                    HeadWidth = 1
                });
            }
        }

        private IEnumerable<DataPoint> MakePoints(int total)
        {
            Random rand = new Random();

            for (int index = 0; index < total; index++)
            {
                double mag = rand.NextDouble();
                double ang = rand.NextDouble() * Math.PI * 2;
                yield return new DataPoint(mag * Math.Cos(ang), mag * Math.Sin(ang));
            }
        }

        private void DrawArrows(IEnumerable<DataPoint> points)
        {
            int index = 0;
            foreach (var point in points)
            {
                PlotModel.Annotations.Add(new ArrowAnnotation
                {
                    StartPoint = point,
                    EndPoint = _zeroPnt,
                    Color = _colors[index % _colors.Length],
                    HeadLength = 0,
                    HeadWidth = 0,
                    Text = string.Format("Arrow{0}", index)
                });
                ++index;
            }//foreach
        }
    }
}
